/*
 * Copyright (c) 2009, Jan Pastor. All Rights Reserved.
 */

package sk.jp.jitag.proc;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import sk.jp.commons.exception.ApplicationException;
import sk.jp.commons.utils.BackgroundProcess;
import sk.jp.jitag.util.Msg;

/**
 * //TODO Dokoncit!!! - test
 */
public class RenderThumbnailsProcess extends BackgroundProcess {

    private static final Logger log = LoggerFactory.getLogger(RenderThumbnailsProcess.class);

    //----------------------------------------------------------------------

    public RenderThumbnailsProcess(StateHandler... stateHandler) {
        super(stateHandler);
    }

    //----------------------------------------------------------------------

    public boolean isCancellable() {
        return true;
    }

    public String getInfo() {
        return Msg.m.value(); //rendering thumbnails
    }

    public void run() {
        try {
            processStarted();
            for (int i = 0; i < 20; i++) {
                Thread.sleep(1000);
                if(cancelRequest()){
                    processCancelled();
                    return;
                }
            }

            //todo test for exception handling
            if(true){
                throw new ApplicationException("Can't read unsupported image type",
                                               "err.read.uif");
            }

            processFinished();
        }catch(Throwable t){
            log.error("Unexpected exception during performing background process", t);
            processFinishedWithError(t);
        }
    }
}
